package com.groupbuying.store.service.impl;

import com.groupbuying.store.connector.CoustomerFeign;
import com.groupbuying.store.connector.WechatAliFeign;
import com.groupbuying.store.constants.StoreAccountWxinfoRelEnumConstants;
import com.groupbuying.store.dao.*;
import com.groupbuying.store.domain.StoreAccountWxinfoRelDO;
import com.groupbuying.store.service.StoreAccountService;
import com.groupbuying.store.service.StoreAccountWxinfoRelService;
import com.groupbuying.store.utils.JwtUtils;
import com.groupbuying.store.utils.apiresult.ApiResult;
import com.groupbuying.store.utils.apiresult.ApiResultCode;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.*;

@Service
public class StoreAccountWxinfoRelServiceImpl implements StoreAccountWxinfoRelService {
	public static final Logger logger = LoggerFactory.getLogger(StoreAccountWxinfoRelServiceImpl.class);
	@Autowired
	private StoreAccountWxinfoRelDao storeAccountWxinfoRelDao;
	@Autowired
	private StoreAccountDao storeAccountDao;
	@Autowired
	private CoustomerFeign coustomerFeign;
	@Autowired
	StoreAccountService storeAccountService;
	@Autowired
	StoreCheckoutCounterDao storeCheckoutCounterDao;
	@Autowired
	StoreCheckoutCounterRelDao storeCheckoutCounterRelDao;
	@Autowired
	StoreAccountShopRelDao storeAccountShopRelDao;
	@Autowired
	private WechatAliFeign wechatAliFeign;
	@Override
	public StoreAccountWxinfoRelDO get(BigInteger id){
		return storeAccountWxinfoRelDao.get(id);
	}
	
	@Override
	public List<StoreAccountWxinfoRelDO> list(Map<String, Object> map){
		return storeAccountWxinfoRelDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return storeAccountWxinfoRelDao.count(map);
	}
	
	@Override
	public int save(StoreAccountWxinfoRelDO storeAccountWxinfoRel){
		return storeAccountWxinfoRelDao.save(storeAccountWxinfoRel);
	}
	
	@Override
	public int update(StoreAccountWxinfoRelDO storeAccountWxinfoRel){
		return storeAccountWxinfoRelDao.update(storeAccountWxinfoRel);
	}
	
	@Override
	public int remove(BigInteger id){
		return storeAccountWxinfoRelDao.remove(id);
	}
	
	@Override
	public int batchRemove(BigInteger[] ids){
		return storeAccountWxinfoRelDao.batchRemove(ids);
	}

	@Override
	public ApiResult getByAccountAndOpen(Map<String, Object> param) {
		ApiResult apiResult = storeAccountService.loginForStore(param);

		if (ApiResultCode.FAILURE.code().equals("0")) {
			return apiResult;
		}
		//TODO为后续存储获取微信信息桌准备
		StoreAccountWxinfoRelDO storeAccountWxinfoRel = new StoreAccountWxinfoRelDO();
		String code = param.get("code").toString();
		Map<String, Object> res = (Map<String, Object>) apiResult.getResult();
		String accountId = "";
		try {

			accountId = JwtUtils.getInfoFromToken(res.get("token").toString()).getUserId();
		} catch (Exception e) {
			e.printStackTrace();
		}
		Map<String,Object> resOpenId = wechatAliFeign.getMPOpenidByCode(code,"youdianproductor");
		String  openId = resOpenId.get("openid")==null?null:resOpenId.get("openid").toString();
		if(StringUtils.isEmpty(openId)){
			logger.info("用户：{}登陆成功,code：{},openId获取失败",accountId,code);
			return apiResult;
		}
		StoreAccountWxinfoRelDO storeAccountWxinfoRelDO = storeAccountWxinfoRelDao.getByAccountAndOpen(accountId, openId);
		if(storeAccountWxinfoRelDO !=null){
			return apiResult;
		}else{
			storeAccountWxinfoRel.setAccountId(accountId);
			storeAccountWxinfoRel.setOpenId(openId);
			storeAccountWxinfoRel.setGmtCreate(new Date());
			storeAccountWxinfoRel.setGmtModified(new Date());
			storeAccountWxinfoRel.setIsDelete(StoreAccountWxinfoRelEnumConstants.ENUM_ISDELETE_UNDEL);
			storeAccountWxinfoRelDao.save(storeAccountWxinfoRelDO);
		}
		Map<String,Object> map = new HashMap<>();
		map.put("openId",openId);
		map.put("apiResult",apiResult);
		return ApiResult.success(map);
	}

	@Override
	public int removeByAccountIdAndOpenId(Map<String, String> param) {
		String accountId = param.get("accountId");
		String openId = param.get("openId");
		return storeAccountWxinfoRelDao.removeByAccountIdAndOpenId(accountId, openId);

	}

	@Override
	public Map<String, Set<String>> getOpenAndAccountByCounterId(String counterId) {
		List<String> staffUuidList = storeCheckoutCounterRelDao.getStaffUuidByCounterId(counterId);
		Set<String> openIdList=null;
		try{
			if(staffUuidList != null && staffUuidList.size()>0){
				openIdList =storeAccountWxinfoRelDao.getOpenIdByAccount(staffUuidList);
			}
		}catch (Exception e){
			e.printStackTrace();
			return null;
		}
		Map<String,Set<String>> parma = new HashMap<>();
		 parma.put("openId",openIdList);
		 return parma;

	}

	@Override
	public int removeByMobile(String mobile) {
		if(!StringUtils.isEmpty(mobile)){
			List<String> accountByMobile = storeAccountDao.getAccountByMobile(mobile);
			return storeAccountWxinfoRelDao.removeByAccountId(accountByMobile);
		}
		return  0;
	}

	@Override
	public ApiResult deleteByOpenId(String code) {
		Map<String,Object> resOpenId = wechatAliFeign.getMPOpenidByCode(code,"youdianproductor");
		String openId = resOpenId.get("openid")==null?null:resOpenId.get("openid").toString();
		logger.info("获取的openId为:{}",openId);
		int i = storeAccountWxinfoRelDao.deleteByOpenId(openId);
		return ApiResult.success(i);

	}

}
